#include <iostream>
#include <cerrno>
#include <string>

#include "sock.hpp"
#include "protocol.hpp"

void usage(std::string proc)
{
  std::cout << "usage:\n\t" << proc << ": need server_ip server_port" << std::endl;
}

int main(int argc, char* argv[])
{
  if (argc != 3)
  {
    usage(argv[0]);
    return 1;
  }

  int sock = Sock::Socket();
  Sock::Connect(sock, argv[1], atoi(argv[2]));

  while(true)
  {
    //业务逻辑
    request_t req;
    memset(&req, 0, sizeof(req));
    std::cout << "please enter data left# ";
    std::cin >> req.left;
    std::cout << "please enter data right# ";
    std::cin >> req.right;
    std::cout << "please enter data operator# ";
    std::cin >> req.op;

    write(sock, &req, sizeof(req));

    response_t resp;
    ssize_t ret = read(sock, &resp, sizeof(resp));
    if (ret == sizeof(resp))
    {
      std::cout << "code : " << resp.code << std::endl;
      std::cout << "result : " << resp.result << std::endl;
      std::cout << std::endl;
    }
    else
    {
      perror("read");
      std::cout << "errno : " << errno << std::endl;
    }
  }

  return 0;
}
